#pragma semicolon 1
#pragma newdecls required

#include <sourcemod>
#include <left4dhooks>


#define PLUGIN_VERSION "1.0"
ConVar c_bilexy, c_bilez, c_bile;
float g_bilexy, g_bilez;
int g_bile;

public Plugin myinfo =
{
	name = "l4d2_bile_blast",
	author = "豆瓣酱な",
	description = "Boomer胆汁击飞玩家.",
	version = PLUGIN_VERSION,
	url = "https://gitee.com/sweet-and-sour-bombs/adfvsdsd"
};

public void OnPluginStart()
{

	c_bile = CreateConVar("l4d2_c_bile", "1", "是否启用插件?", FCVAR_NOTIFY, true, 0.0, true, 1.0);
	c_bilexy = CreateConVar("l4a2_c_bilexy", "100.0", "玩家Y轴高度.", FCVAR_NOTIFY, true, 0.0);
	c_bilez = CreateConVar("l4d2_c_bilez", "251.0",   "玩家Z轴击飞高度.", FCVAR_NOTIFY, true, 0.0);
	CreateConVar("l4d2_bile_blast_version", PLUGIN_VERSION, "插件版本.", FCVAR_NOTIFY);

	HookConVarChange(c_bile, eConvarChanged);
	HookConVarChange(c_bilexy, eConvarChanged);
	HookConVarChange(c_bilez, eConvarChanged);

	HookEvent("player_now_it", HandlePlayerNowIt, EventHookMode_Pre); //玩家被喷胆汁
	

	//AutoExecConfig(true, "l4d2_bile_blast");
}

public void OnMapStart()
{
	CvarsChanged();
}

public void eConvarChanged(Handle hCvar, const char[] sOldVal, const char[] sNewVal)
{
	CvarsChanged();
}

void CvarsChanged()
{
	g_bile = c_bile.IntValue;
	g_bilexy = c_bilexy.FloatValue;
	g_bilez = c_bilez.FloatValue;

}

//玩家被喷胆汁.
public void HandlePlayerNowIt(Event event, const char[] name, bool dontBroadcast)
{
	if(g_bile == 0)
		return;



	int victim = GetClientOfUserId(event.GetInt("userid"));
	
	if (IsValidClient(victim) && IsPlayerAlive(victim) && GetClientTeam(victim) == 2)	
	{

				float HeadingVector[3], resulting[3], fPos[3];
				GetClientEyeAngles(victim, HeadingVector);
				GetEntPropVector(victim, Prop_Data, "m_vecVelocity", resulting);

				resulting[0] =- Cosine(DegToRad(HeadingVector[1])) * g_bilexy;
				resulting[1] =- Sine(DegToRad(HeadingVector[1])) * g_bilexy;
				resulting[2] =  g_bilez;
				TeleportEntity(victim, NULL_VECTOR, NULL_VECTOR, resulting);
				GetClientAbsOrigin(victim, fPos);
				L4D_StaggerPlayer(victim, victim, fPos); // 踉跄  效果
				L4D2_CTerrorPlayer_Fling(victim, victim, resulting);
				CreateTimer(3.0, Timer_CheckVictim, GetClientUserId(victim));
	}
	


}


public Action Timer_CheckVictim(Handle timer, any userid)
{
	int victim = GetClientOfUserId(userid);

	if (IsValidClient(victim) && IsPlayerAlive(victim) && GetClientTeam(victim) == 2)
	{
		L4D_OnITExpired(victim); //玩家被喷胆汁时立即清除效果
	}
	return Plugin_Stop;
}


//判断是否有效玩家
bool IsValidClient(int client)
{
	return client > 0 && client <= MaxClients && IsClientInGame(client);
}



